require 'rsense/assist'

module RSense
  class Daemon
    attr_reader :code_assist

    def initialize
      @code_assist = CodeAssist.new
    end

    def start
      while line = STDIN.readline
        case line
        when /^$/
        when /^suggest +([^ ]+) +(\d+) +(\d+)$/
          begin
            source = STDIN.read($3.to_i)
            point = $2.to_i
            name = $1
            result = @code_assist.suggest(name, source, point)
            puts "candidates #{result.candidates.size} #{result.prefix}"
            result.candidates.each do |candidate|
              puts candidate
            end
          rescue => ex
            puts "error #{ex}"
          end
        else
          puts 'error unknown command'
        end
      end
    end
  end
end
